﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Threading.Thread;
using static System.Console;

namespace Project_4
{
    /// <summary>
    /// 4.5 将APM模式转换为任务
    /// </summary>
    public class Unit_4_05
    {
        public static void Go()
        {
            int threadId;
            AsynchronousTask d = Test;
            IncompatibleAsynchronousTask e = Test;
            WriteLine("Option 1");
            Task<string> task =
                Task<string>.Factory.FromAsync(d.BeginInvoke("AsyncTaskThread", Callback, "a delegate asynchronous call"), d.EndInvoke);
            task.ContinueWith(t => WriteLine($"回调方法执行完成，现在运行一个延续任务！Result：{t.Result}"));
            while (!task.IsCompleted)
            {
                WriteLine(task.Status);
                Sleep(TimeSpan.FromSeconds(0.5));
            }
            WriteLine(task.Status);
            Sleep(TimeSpan.FromSeconds(1));
            WriteLine("---------------------------------");
            WriteLine();
            WriteLine("Option 2");

            task = Task<string>.Factory.FromAsync(d.BeginInvoke, d.EndInvoke, "AsyncTaskThread",
                "a delegate asynchronous call");
            task.ContinueWith(t => WriteLine($"任务已经完成，现在执行一个延续任务！Result:{t.Result}"));
            while (!task.IsCompleted)
            {
                WriteLine(task.Status);
                Sleep(TimeSpan.FromSeconds(0.5));
            }
            WriteLine(task.Status);
            Sleep(TimeSpan.FromSeconds(1));
            WriteLine("------------------------------");
            WriteLine();
            WriteLine("Option 3");

            IAsyncResult ar = e.BeginInvoke(out threadId, Callback, "a delegate asynchronous call");
            task = Task<string>.Factory.FromAsync(ar, _ => e.EndInvoke(out threadId, ar));
            task.ContinueWith(t => WriteLine($"任务已经完成，现在执行一个延续任务！Result:{t.Result},ThreadId:{threadId}"));
            while (!task.IsCompleted)
            {
                WriteLine(task.Status);
                Sleep(TimeSpan.FromSeconds(0.5));
            }
            WriteLine(task.Status);
            Sleep(TimeSpan.FromSeconds(1));
        }

        delegate string AsynchronousTask(string threadName);
        delegate string IncompatibleAsynchronousTask(out int threadId);

        static void Callback(IAsyncResult ar)
        {
            WriteLine("Starting a callback...");
            WriteLine($"State passed to a callback: {ar.AsyncState}");
            WriteLine($"是否是线程池的线程：{CurrentThread.IsThreadPoolThread}");
            WriteLine($"线程池中工作的线程id :{CurrentThread.ManagedThreadId}");
        }

        static string Test(string threadName)
        {
            WriteLine("Starting...");
            WriteLine($"是否是线程池的线程:{CurrentThread.IsThreadPoolThread}");
            Sleep(TimeSpan.FromSeconds(2));
            CurrentThread.Name = threadName;
            return $"线程名：{CurrentThread.Name}";
        }

        static string Test(out int threadId)
        {
            WriteLine("Starting...");
            WriteLine($"是否是线程池的线程:{CurrentThread.IsThreadPoolThread}");
            Sleep(TimeSpan.FromSeconds(2));
            threadId = CurrentThread.ManagedThreadId;
            return $"线程池中工作的线程id:{threadId}";
        }
    }
}
